package com.xhsj.user.sms.purchase.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xhsj.user.sms.base.BaseService;
import com.xhsj.user.sms.purchase.entity.TbPurchaseSubtaskQualityRecord;
import com.xhsj.user.sms.purchase.mapper.TbPurchaseSubtaskQualityRecordMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 采购订单-质检记录表 服务实现类
 * </p>
 *
 * @author suxiaolin
 * @since 2020-04-30
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
//@CacheConfig(cacheNames = "TbPurchaseOrderQualityRecord")
public class TbPurchaseSubtaskQualityRecordService extends BaseService<TbPurchaseSubtaskQualityRecordMapper, TbPurchaseSubtaskQualityRecord>{
    @Autowired
    private TbPurchaseSubtaskQualityRecordMapper dao;

    @Override
    //@CacheEvict(cacheNames="TbPurchaseOrderQualityRecord", allEntries=true)
    public boolean save(TbPurchaseSubtaskQualityRecord entity) {
        return super.save(entity);
    }

    @Override
    //@Cacheable(key = "#id",sync = true)
    public TbPurchaseSubtaskQualityRecord getById(Serializable id) {
        return super.getById(id);
    }

    @Override
    //@CacheEvict(cacheNames="TbPurchaseOrderQualityRecord", allEntries=true)
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }

    //@CacheEvict(cacheNames="SysDict", allEntries=true)
    @Override
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        return super.removeByIds(idList);
    }

    //@Cacheable(key = "#data",sync = true)
    public IPage<TbPurchaseSubtaskQualityRecord> selectPage(Map<String,Object> data) {
        int currentPage=1;
        if (!Objects.isNull(data.get("page"))) {
            currentPage=(Integer) data.get("page");
        }
        int pageSize=20;
        if (!Objects.isNull(data.get("size"))) {
            pageSize=(Integer) data.get("size");
        }

        TbPurchaseSubtaskQualityRecord entity = JSON.parseObject(JSON.toJSONString(data), TbPurchaseSubtaskQualityRecord.class);
        Page<TbPurchaseSubtaskQualityRecord> page = new Page(currentPage,pageSize);
        QueryWrapper<TbPurchaseSubtaskQualityRecord> wrapper = new QueryWrapper(entity);
        return super.selectPage(page, wrapper);
    }

    @Override
    //@Cacheable(value = "TbPurchaseOrderQualityRecord",sync = true)
    public List<TbPurchaseSubtaskQualityRecord> selectAll() {
        return super.selectAll();
    }

    //质检记录相关的数据
    public List<Map<String,Object>> getOrderQualityRecordList(String dataId) {
        return dao.getOrderQualityRecordList(dataId);
    }

    //获取质检记录良品，特采，退货数量
    public  List<Map<String,Object>> getOrderQualityRecordNumList(String dataId,int type) {
        return dao.getOrderQualityRecordNumList(dataId,type);
    }

    //获取待入库数量
    public List<Map<String, Object>> getWarehousingNumberList(String dataId) {
       return dao.getWarehousingNumberList(dataId);
    }

    //获取质检最大批次
    public String getMaxBatch(String dataId) {
        return dao.getMaxBatch(dataId);
    }

    //数据库中的待入库量
    public int getWaitStorageNumber(String dataId, String purchaseSubtaskListId) {
        return dao.getWaitStorageNumber(dataId,purchaseSubtaskListId);
    }

    //修改质检记录表中入库状态(0 未完成 1 已完成)
    public boolean updateStorageStatus(int storageStatus, Integer recordBatch, String dataId) {
        TbPurchaseSubtaskQualityRecord tbPurchaseSubtaskQualityRecord=new TbPurchaseSubtaskQualityRecord();
        tbPurchaseSubtaskQualityRecord.setStorageStatus(storageStatus);
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.lt("batch",recordBatch);
        queryWrapper.eq("purchase_subtask_id",dataId);
        if(dao.updateStorageStatus(storageStatus, recordBatch,dataId)==0){
            return false;
        }
        return true;
    }

    //获取入库时质检记录
    public List<Map<String, Object>> getWarehousingQualityRecordList(String dataId) {
      return   dao.getWarehousingQualityRecordList(dataId);
    }
}
